<template>
  <div style="min-height: 500px">
    <el-form ref="planForm" :model="entity" :rules="rules" label-width="120px">
      <el-form-item label="计划名称" prop="title">
        <el-input v-model="entity.title" placeholder="计划名称" ref="title" />
      </el-form-item>

      <el-form-item label="周期类型" prop="zq">
        <el-select v-model="entity.zq" placeholder="周期类型" @change="zqChange" style="width: 100%;" >
          <el-option v-for="item in state.zq" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="盘点开始日期" prop="cdefine1" v-if="state.PlanZq.once === entity.zq">
        <el-date-picker v-model="entity.cdefine1" type="date" placeholder="盘点开始日期" :disabled-date="disabledDate" style="width: 100%;" value-format="YYYY-MM-DD" />
      </el-form-item>
      <el-form-item label="盘点截止日期" prop="cdefine2" v-if="state.PlanZq.once === entity.zq">
        <el-date-picker v-model="entity.cdefine2" type="date" placeholder="盘点截止日期" :disabled-date="disabledDate" style="width: 100%;" value-format="YYYY-MM-DD" />
      </el-form-item>
      <el-form-item label="季度月份类型" prop="cdefine3" v-if="state.PlanZq.jidu === entity.zq">
        <el-select v-model="entity.cdefine3" placeholder="季度月份类型"  style="width: 100%;" >
          <el-option v-for="item in state.jdMonth" :key="item.id" :label="item.label" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="盘点开始日期" prop="cdefine1" v-if="state.PlanZq.month === entity.zq || state.PlanZq.jidu === entity.zq">
        <el-date-picker v-model="entity.cdefine1" type="date" placeholder="盘点开始日期" style="width: 100%;" value-format="DD" format="DD" />
      </el-form-item>
      <el-form-item label="盘点截止日期" prop="cdefine2" v-if="state.PlanZq.month === entity.zq || state.PlanZq.jidu === entity.zq">
        <el-date-picker v-model="entity.cdefine2" type="date" placeholder="盘点截止日期" style="width: 100%;" value-format="DD" format="DD" />
      </el-form-item>

      <el-form-item label="盘点类型" prop="pdlx">
        <el-select v-model="entity.pdlx" placeholder="盘点类型" style="width: 100%;" >
          <el-option v-for="item in state.pdlx" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="最小盘点比率" prop="cdefine4" v-if="state.PlanPdlx.choujian === entity.pdlx">
        <el-input-number v-model="entity.cdefine4" :precision="0" :step="1" :max="100" placeholder="最小盘点比率" style="width: 100%;"/>
      </el-form-item>
      <el-form-item label="最大重复比率" prop="cdefine5" v-if="state.PlanPdlx.choujian === entity.pdlx">
        <el-input-number v-model="entity.cdefine5" :precision="0" :step="1" :max="100" placeholder="最大重复比率" style="width: 100%;"/>
      </el-form-item>

      <el-form-item label="业务类型" prop="ywlx">
        <el-select v-model="entity.ywlx" placeholder="业务类型" style="width: 100%;" >
          <el-option v-for="item in state.ywlx" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="entity.remark" type="textarea" placeholder="备注" />
      </el-form-item>
      <el-form-item label="盘点范围">
        <el-checkbox-group v-model="state.selectedCkList">
          <el-checkbox :label="item.cwhname" v-for="(item,index) in state.cangKuList"  :key="item.id" />
        </el-checkbox-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="btnSubmit" size="default">确 定</el-button>
        <el-button @click="onCancel" size="default">取 消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup name="addPlan">
import request from '/@/utils/request';
import { ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import PlanZq from "/@/utils/PlanZq";
import PlanPdlx from "/@/utils/PlanPdlx";
import dayjs from "dayjs";
import {reactive, ref, onMounted, defineProps, defineEmits, nextTick} from "vue";

const planForm = ref(null)
const title = ref(null)
const emit = defineEmits(['update:modelValue'])
let props = defineProps({
  modelValue : {type : Boolean, default: true},
})
let entity = ref({
  title : '',
  zq:undefined,
  pdlx:undefined,
  ywlx:undefined,
  cdefine1:'',
  cdefine2:'',
  cdefine3:1,
  cdefine4:'',
  cdefine5:'',
  remark:'',
})
const rules = reactive({
  title: [
    { required: true, message: '请输入计划名称', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  zq: [
    { required: true, message: '请选择周期类型', trigger: 'blur' }
  ],
  pdlx: [
    { required: true, message: '请选择盘点类型', trigger: 'blur' }
  ],
  ywlx: [
    { required: true, message: '请选择业务类型', trigger: 'blur' }
  ],
  cdefine1: [
    { required: true, message: '盘点开始日期', trigger: 'blur' }
  ],
  cdefine2: [
    { required: true, message: '盘点截止日期', trigger: 'blur' }
  ],
})
const state = reactive({
  zq : [],
  ywlx:[],
  pdlx:[],
  jdMonth : [{id : 1,label: "季度初"}, {id : 2,label: "季度中"}, {id : 3,label: "季度末"}],
  cangKuList:[],
  selectedCkList : [],
  PlanZq : PlanZq,
  PlanPdlx : PlanPdlx,
})
const disabledDate = (time) => {
  const today =  new Date(dayjs().add(-1, 'day').format('YYYY-MM-DD'))
  return time.getTime() < today.valueOf();
}
const loadType = () => {
  request({
    url: '/plan/getplantypeList',
    data: {}
  }).then( res =>{
    const {zq, ywlx, pdlx} = res.data
    state.zq = zq;
    state.ywlx = ywlx;
    state.pdlx = pdlx;
  })
}
const searchUserCangKu = () =>{
  request({
    url: '/cangku/listuserwh',
    data: {}
  }).then(res => {
    const { data } = res
    state.cangKuList = data
  })
}
const btnSubmit = () => {
  if(state.selectedCkList.length <=0){
    ElMessage.error("请选择盘点仓库")
    return;
  }
  if(!planForm.value) return;
  let selectedCkIds = []
  state.selectedCkList.forEach(cwhname => {
    let fArray = state.cangKuList.filter(ry => ry.cwhname === cwhname)
    fArray.forEach(fry =>{
      selectedCkIds.push(fry.id)
    })
  })
  entity.value.selectedCkIds = selectedCkIds
  planForm.value.validate((valid)=>{
    if(valid){
      request({
        url: '/plan/addplan',
        data: entity.value
      }).then( res =>{
        if(res.code === "200"){
          ElMessage.success(res.msg)
          emit('update:modelValue', false) //关闭弹窗
          mittBus.emit('planadd', {})//触发事件总线，让列表界面更新数据
        }else{
          ElMessage.error(res.msg);
        }
      })
    }else{
      console.log('error submit!!');
      return false;
    }
  })
}
const onCancel = ()=>{
  emit('update:modelValue', false) //关闭弹窗
}
const zqChange = (value)=>{

}
onMounted(()=>{
  loadType();
  searchUserCangKu();
  nextTick(()=>{
    title.value.focus();
  })
})
</script>

<style scoped>

</style>
